//
//  UIBarButtonItem+Extension.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/18.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    ///  设置一个模板构建方法，设置参数的默认值，方便应对不同的业务逻辑
    convenience init(imageNamed:String? = nil, title:String? = nil, target:AnyObject?, action:Selector) {
        //调用同级的默认初始化函数
        self.init()
        
        //开始设置多个值
        let button = UIButton()
            //图片一般是自定义箭头等
        if let imgN = imageNamed {
            button.setImage(UIImage(named: imgN), forState: .Normal)
            button.setImage(UIImage(named: "\(imgN)_highlighted"), forState: .Highlighted)
        }
        
        if let t = title {
            button.setTitle(t, forState: .Normal)
            button.setTitleColor(UIColor.grayColor(), forState: .Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
        }
        //别忘了确定button的frame
        button.sizeToFit()
        button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        //将customView赋值以后，该方法返回的item对象就会采用customView设置的对象
        customView = button
    }
}
